using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CanMoveBlock : MonoBehaviour
{
    Transform stander;
    public float maxXZ = 50;
    public float maxY = 50;

    protected Vector3 OrigPos;
    protected int layer = 0;
    protected int treeLayer = 0;
    [HideInInspector]
    public bool IsOverlap = false;


    protected virtual void Start()
    {
        layer = LayerMask.GetMask("Block");
        treeLayer = LayerMask.GetMask("Tree");

        MessageManager.BindAction(MsgKey.ClickRight, RecordOriPos);
        MessageManager.BindAction(MsgKey.ClickForward, RecordOriPos);
        MessageManager.BindAction(MsgKey.ClickUp, RecordOriPos);
        MessageManager.BindAction(MsgKey.FindStander, FindStander);

        MessageManager.BindAction(MsgKey.MoveToRight, MoveToRight);
        MessageManager.BindAction(MsgKey.MoveToForward, MoveToForward);
        MessageManager.BindAction(MsgKey.MoveToUp, MoveToTop);
        MessageManager.BindAction(MsgKey.MoveableMoveToOrig, MoveToOrig);
    }

    protected virtual void OnDestroy()
    {
        MessageManager.Remove(MsgKey.ClickRight, RecordOriPos);
        MessageManager.Remove(MsgKey.ClickForward, RecordOriPos);
        MessageManager.Remove(MsgKey.ClickUp, RecordOriPos);
        MessageManager.Remove(MsgKey.FindStander, FindStander);

        MessageManager.Remove(MsgKey.MoveToRight, MoveToRight);
        MessageManager.Remove(MsgKey.MoveToForward, MoveToForward);
        MessageManager.Remove(MsgKey.MoveToUp, MoveToTop);
        MessageManager.Remove(MsgKey.MoveableMoveToOrig, MoveToOrig);
    }

    protected virtual void SetPosition(Vector3 pos)
    {
        transform.position = pos;
    }

    public void FindStander()
    {
        Ray ray = new Ray(transform.position + Vector3.up, Vector3.down);
        if (Physics.Raycast(ray, out RaycastHit hit, 4, layer))
        {
            stander = hit.transform;
        }
        if (tag == "Stone")
        {
            print(stander);
        }
    }

    void RecordOriPos()
    {
        OrigPos = transform.position;
    }

    protected virtual void MoveToRight()
    {
        Ray ray1 = new Ray(transform.position + Vector3.up * 0.5f, Vector3.left);
        Ray ray2 = new Ray(transform.position + Vector3.up * 0.5f, Vector3.right);
        if (Physics.Raycast(ray2, 100, layer | treeLayer))
        {
            IsOverlap = true;
            SetPosition(new Vector3(maxXZ - 3, OrigPos.y, OrigPos.z));
        }
        else
        {
            if (Physics.Raycast(ray1, 100, layer | treeLayer))
            {
                SetPosition(new Vector3(maxXZ + 3, OrigPos.y, OrigPos.z));
                IsOverlap = true;
            }
            else
            {
                SetPosition(new Vector3(maxXZ, OrigPos.y, OrigPos.z));
            }
        }
    }

    protected virtual void MoveToForward()
    {
        Ray ray1 = new Ray(transform.position + Vector3.up * 0.5f, Vector3.forward);
        Ray ray2 = new Ray(transform.position + Vector3.up * 0.5f, Vector3.back);
        if (Physics.Raycast(ray2, 100, layer | treeLayer))
        {
            IsOverlap = true;
            SetPosition(new Vector3(OrigPos.x, OrigPos.y, -maxXZ + 3));
        }
        else
        {
            if (Physics.Raycast(ray1, 100, layer | treeLayer))
            {
                SetPosition(new Vector3(OrigPos.x, OrigPos.y, -maxXZ - 3));
                IsOverlap = true;
            }
            else
            {
                SetPosition(new Vector3(OrigPos.x, OrigPos.y, -maxXZ));
            }
        }
    }

    protected virtual void MoveToTop()
    {
        Ray ray = new Ray(transform.position, Vector3.up);
        if (Physics.Raycast(ray, 100, layer))
        {
            gameObject.SetActive(false);
        }
        else
        {
            gameObject.SetActive(true);
        }
        SetPosition(new Vector3(OrigPos.x, maxY, OrigPos.z));
    }

    protected virtual void MoveToOrig()
    {
        IsOverlap = false;
        gameObject.SetActive(true);
        if (CameraController.cameralook == CameraLook.Right)
        {
            Vector3 newPos = transform.position;
            newPos.x = OrigPos.x;
            SetPosition(newPos);

            Ray ray = new Ray(transform.position + Vector3.up, Vector3.down);
            if (!Physics.Raycast(ray, 3, layer))
            {
                SetPosition(stander.position + Vector3.up * 1.5f);
            }

        }
        else if (CameraController.cameralook == CameraLook.Forward)
        {
            Vector3 newPos = transform.position;
            newPos.z = OrigPos.z;
            SetPosition(newPos);

            Ray ray = new Ray(newPos + Vector3.up, Vector3.down);
            if (!Physics.Raycast(ray, 3, layer))
            {
                SetPosition(stander.position + Vector3.up * 1.5f);
            }
        }
        else if (CameraController.cameralook == CameraLook.Top)
        {
            Vector3 newPos = transform.position;
            newPos.y = OrigPos.y;
            SetPosition(newPos);

            if (Mathf.Abs(transform.position.x -OrigPos.x )<0.5f && Mathf.Abs(transform.position.z - OrigPos.z) < 0.5f)
            {
                transform.position = OrigPos;
                return;
            }

            if(stander!=null)
            {
                transform.position = stander.position + new Vector3(0, 1.5f, 0);
            }
            else
            {
                transform.position = OrigPos;
            }
        }
    }
}
